#Region "Imports"
Imports System.IO
Imports pdflib
#End Region
''' <summary>
''' This module shows how to use the PDF library.
''' It is also useful to test the library.
''' </summary>
''' <remarks></remarks>
Module Main
    Private log As ClsLog
    Private image1, image2 As pdflib.clsPDFImage
    Sub Main()
        Try

            'Example_1_Display_Information("test_pdf\Jun01ResNews.pdf")
            'Example_2_Display_Page_Information("C:\Users\Charles\Desktop\FTP\pdflibtest\pdf_test\Bulletin_printemps_2010.pdf")
            'Example_3_Modify_Pdf_Information("C:\Users\Charles\Desktop\FTP\pdflibtest\pdf_test\Bulletin_printemps_2010.pdf")
            'Example_4_Add_Annotation("C:\Users\Charles\Desktop\FTP\pdflibtest\pdf_test\Bulletin_printemps_2010.pdf")
            'Example_5_Add_Image("d:\pdf\gj-oopsla.pdf")
            'Exemple_6_Modify_Page("d:\pdf\273.pdf")

            image1 = New clsPDFImage("C:\Users\Charles\Desktop\FTP\pdflibtest\ios.jpg")
            image2 = New clsPDFImage("C:\Users\Charles\Desktop\FTP\pdflibtest\images.jpg")
            log = New ClsLog("C:\Users\Charles\Desktop\FTP\pdflibtest\pdflib_log.txt")
            BatchTest()     'Batch test for the library
            'ShowPdfInfo("D:\pdf_test\Plaquette_Formation_Continue.pdf")
            'MovePOFEncrypted()
            'DebugTest("D:\pdf_test\Plaquette_Formation_Continue.pdf")
            log.Close()

        Catch ex As Exception
            Stop
        End Try
    End Sub
    Private Sub MovePOFEncrypted()
        Dim fichier As FileInfo

        Dim source As New DirectoryInfo(My.Resources.dir_pdf_test)
        Dim destination As New DirectoryInfo(My.Resources.dir_pdf_test_encrypted)

        For Each fichier In source.GetFiles
            If fichier.Extension.ToLower = ".pdf" Then
                Dim myPDF As New pdflib.ClsPDF(fichier.FullName)
                If myPDF.IsEncrypt Then
                    myPDF.Close()
                    fichier.MoveTo(destination.FullName + "\" + fichier.Name)
                End If
            End If
        Next

    End Sub
    ''' <summary>
    ''' This example shows how to display basic information on a PDF
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Example_1_Display_Information(ByVal filePath As String)
        Dim myPDF As New pdflib.ClsPDF(filePath)
        Console.WriteLine("Information for {0}", myPDF.FileInfo.FullName)
        Console.WriteLine("  {0}", myPDF.Version)
        Console.WriteLine("  {0} pages", myPDF.PageCount)
        If myPDF.IsLinearized Then
            Console.WriteLine("  is linearized")
        End If
        If myPDF.IsEncrypt Then
            Console.WriteLine("  encrypted")
            Console.WriteLine("    Filter:             {0}", myPDF.EncryptionInformation.Filter)
            If myPDF.EncryptionInformation.HasUserRights Then
                Console.WriteLine("    User Rights:")
                Console.Write("      Print:                            {0} ", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanPrint))
                If myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanPrintHighQuality) Then
                    Console.WriteLine("(original quality)")
                Else
                    Console.WriteLine("(low quality)")
                End If
                Console.WriteLine("      Content modification:             {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanModifyContent))
                Console.WriteLine("      Content extraction:               {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanExtractTextAndGraphic))
                Console.WriteLine("      Annotations:                      {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanTextAnnotation))
                Console.WriteLine("      Form filling:                     {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanFillForm))
                Console.WriteLine("      Assemble document:                {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanAssembleDocument))
            End If
        End If
        'If the PDF is encrypted (password protected), the documentInformation elements may be also encrypted
        Console.WriteLine("  pdf information:")
        Console.WriteLine("    Title:             {0}", myPDF.DocumentInformation.Title)
        Console.WriteLine("    Author:            {0}", myPDF.DocumentInformation.Author)
        Console.WriteLine("    Subject:           {0}", myPDF.DocumentInformation.Subject)
        Console.WriteLine("    Keywords:          {0}", myPDF.DocumentInformation.Keywords)
        Console.WriteLine("    Creator:           {0}", myPDF.DocumentInformation.Creator)
        Console.WriteLine("    Producer:          {0}", myPDF.DocumentInformation.Producer)
        Console.WriteLine("    Creation Date:     {0}", myPDF.DocumentInformation.CreationDate)
        Console.WriteLine("    Modification Date: {0}", myPDF.DocumentInformation.GetModificationDate)
        Stop
    End Sub

    ''' <summary>
    ''' This example shows how to display information for a page in the PDF
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Example_2_Display_Page_Information(ByVal filePath As String)
        Dim page As pdflib.clsPDFPage
        Dim myPDF As New pdflib.ClsPDF(filePath)
        Console.WriteLine("  {0} pages", myPDF.PageCount)
        For Each page In myPDF.GetPages
            Console.WriteLine("  information for page number {0}:", page.Number)
            Console.WriteLine("    User Unit:  {0}", page.UserUnit)
            Console.WriteLine("    Page Width: {0}", page.Width)
            Console.WriteLine("    Page Rotation: {0}", page.Rotation)
        Next
        Stop
    End Sub


    ''' <summary>
    ''' This example shows how to modify the PDF document information properties
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Example_3_Modify_Pdf_Information(ByVal filePath As String)
        Dim myPDF As New pdflib.ClsPDF(filePath)
        If Not myPDF.IsEncrypt Then
            'We can modify the PDF if it is not encrypted
            myPDF.DocumentInformation.Keywords = "Some keywords here"
            myPDF.DocumentInformation.Author = "Unknown"
            myPDF.DocumentInformation.Title = "A Title"
            myPDF.DocumentInformation.Producer = "pdflib"
            myPDF.DocumentInformation.Subject = "A subject"
            myPDF.DocumentInformation.Creator = "Unknown"
            myPDF.Save()
            Stop
        End If
    End Sub


    ''' <summary>
    ''' This example shows how to add an annotation to a PDF
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Example_4_Add_Annotation(ByVal filePath As String)
        Dim page As pdflib.clsPDFPage

        Dim myPDF As New pdflib.ClsPDF(filePath)

        If Not myPDF.IsEncrypt Then
            Dim myAnnotation As New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
            myAnnotation.Location(300, 300, 300, 300)
            myAnnotation.Contents = String.Format("This is an annotation for the PDF {0}", myPDF.FileInfo.Name)
            myAnnotation.SetCharacteristic = pdflib.AnnotationFlags.ReadOnlyFlag Or pdflib.AnnotationFlags.Locked

            'We add the annotation on each page of the PDF!
            For Each page In myPDF.GetPages
                page.AddAnnotation(myAnnotation)
            Next

            myPDF.Save(My.Resources.dir_pdf_saved + myPDF.FileInfo.Name)
        End If
        Stop
    End Sub
    ''' <summary>
    ''' This example shows how to add an image to a PDF
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Example_5_Add_Image(ByVal filePath As String)
        Dim page As pdflib.clsPDFPage

        Dim myPDF As New pdflib.ClsPDF(filePath)

        If Not myPDF.IsEncrypt Then
            Dim myImage As New pdflib.clsPDFImage("C:\Program Files\Serena\vm\common\SampleDB\work\dev\checkers\client\images\PIECES.JPG")

            'We add the image on each page of the PDF!
            For Each page In myPDF.GetPages
                page.AddImage(myImage, 200, 200, 100, 100)
            Next

            'We add another image only on the first page
            Dim image2 As New pdflib.clsPDFImage("C:\Program Files\Serena\vm\common\SampleDB\work\dev\checkers\client\images\PIECES.JPG")
            page = myPDF.GetPage(1)
            page.AddImage(image2, 100, 100, 25, 25)

            myPDF.Save(My.Resources.dir_pdf_saved + myPDF.FileInfo.Name)
        End If
        Stop
    End Sub
    ''' <summary>
    ''' This example shows how to modify characteristics of a page
    ''' </summary>
    ''' <param name="filePath"></param>
    ''' <remarks></remarks>
    Private Sub Exemple_6_Modify_Page(ByVal filePath As String)
        Dim page As pdflib.clsPDFPage
        Dim myPDF As New pdflib.ClsPDF(filePath)
        If Not myPDF.IsEncrypt Then
            page = myPDF.GetPage(1)
            If Not page Is Nothing Then
                'We rotation page 1
                page.Rotation = pdflib.PageRotationTypes.Rotation_180
            End If
            page = myPDF.GetPage(2)
            If Not page Is Nothing Then
                'We erase the content of page 2
                page.EraseContent()
            End If
            myPDF.Save(My.Resources.dir_pdf_saved + myPDF.FileInfo.Name)
        End If
        Stop
    End Sub
    ''' <summary>
    ''' I use this method to test the library against hundreds of PDFs stored in a folder
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BatchTest()

        Dim folder As New DirectoryInfo(My.Resources.dir_pdf_test)
        ProcessPdfFolder(folder)

    End Sub
    Private Sub ProcessPdfFolder(ByVal folder As DirectoryInfo)
        Dim fichier As FileInfo
        Dim dir As DirectoryInfo
        For Each fichier In folder.GetFiles
            If fichier.Extension.ToLower = ".pdf" Then
                ShowPdfInfo(fichier)
            End If
        Next
        For Each dir In folder.GetDirectories
            ProcessPdfFolder(dir)
        Next
    End Sub
    Private Sub ShowPdfInfo(filePath As String)
        Dim f As New FileInfo(filePath)
        If f.Exists Then
            ShowPdfInfo(f)
        End If
    End Sub



    Private Sub ShowPdfInfo(ByVal fichier As FileSystemInfo)
        Console.WriteLine("{0}", fichier.FullName)
        log.Stream.WriteLine("{0}", fichier.FullName)

        Dim myPDF As New ClsPDF(fichier.FullName)

        log.Stream.WriteLine("  {0}", myPDF.Version)
        log.Stream.WriteLine("  {0} pages", myPDF.PageCount)

        If myPDF.IsLinearized Then
            log.Stream.WriteLine("  linearized")
        End If

        If myPDF.IsEncrypt Then
            log.Stream.WriteLine("  encrypted")
            log.Stream.WriteLine("    Filter:             {0}", myPDF.EncryptionInformation.Filter)
            If myPDF.EncryptionInformation.HasUserRights Then
                log.Stream.WriteLine("    User Rights:")
                log.Stream.Write("      Print:                            {0} ", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanPrint))
                If myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanPrintHighQuality) Then
                    log.Stream.WriteLine("(original quality)")
                Else
                    log.Stream.WriteLine("(low quality)")
                End If
                log.Stream.WriteLine("      Content modification:             {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanModifyContent))
                log.Stream.WriteLine("      Content extraction:               {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanExtractTextAndGraphic))
                log.Stream.WriteLine("      Annotations:                      {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanTextAnnotation))
                log.Stream.WriteLine("      Form filling:                     {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanFillForm))
                log.Stream.WriteLine("      Assemble document:                {0}", myPDF.EncryptionInformation.UserRight(pdflib.UserAccess.CanAssembleDocument))
            End If
        End If

        If Not myPDF.ViewerPreferences Is Nothing Then
            log.Stream.WriteLine("  Viewer preferences:")
            log.Stream.WriteLine("    Direction:             {0}", myPDF.ViewerPreferences.Direction.ToString)
            log.Stream.WriteLine("    Hide Toolbar:          {0}", myPDF.ViewerPreferences.HideToolbar)
            log.Stream.WriteLine("    Center Window:         {0}", myPDF.ViewerPreferences.CenterWindow)
            log.Stream.WriteLine("    Hide Menubar:          {0}", myPDF.ViewerPreferences.HideMenubar)
            log.Stream.WriteLine("    Hide Window UI:        {0}", myPDF.ViewerPreferences.HideWindowUI)
            log.Stream.WriteLine("    Fit Window:            {0}", myPDF.ViewerPreferences.FitWindow)
            log.Stream.WriteLine("    Display Doc Title:     {0}", myPDF.ViewerPreferences.DisplayDocTitle)
            log.Stream.WriteLine("    Print Scaling:         {0}", myPDF.ViewerPreferences.PrintScaling.ToString)
            log.Stream.WriteLine("    Non Full Screen Page Mode: {0}", myPDF.ViewerPreferences.NonFullScreenPageMode.ToString)
            log.Stream.WriteLine("    Duplex:                {0}", myPDF.ViewerPreferences.Duplex.ToString)
            log.Stream.WriteLine("    Pick Tray by PDF Size: {0}", myPDF.ViewerPreferences.PickTrayByPDFSize)
            log.Stream.WriteLine("    Number of Copies:      {0}", myPDF.ViewerPreferences.NumCopies)
            log.Stream.WriteLine("    Page Layout:           {0}", myPDF.ViewerPreferences.PageLayout.ToString)
            log.Stream.WriteLine("    Page Mode:             {0}", myPDF.ViewerPreferences.PageMode.ToString)
        End If

        If Not myPDF.IsEncrypt Then

            'We can modify the PDF if it is not encrypted

            log.Stream.WriteLine("  pdf information:")
            log.Stream.WriteLine("    Titre:             {0}", myPDF.DocumentInformation.Title)
            log.Stream.WriteLine("    Auteur:            {0}", myPDF.DocumentInformation.Author)
            log.Stream.WriteLine("    Subject:           {0}", myPDF.DocumentInformation.Subject)
            log.Stream.WriteLine("    Keywords:          {0}", myPDF.DocumentInformation.Keywords)
            log.Stream.WriteLine("    Creator:           {0}", myPDF.DocumentInformation.Creator)
            log.Stream.WriteLine("    Producer:          {0}", myPDF.DocumentInformation.Producer)
            log.Stream.WriteLine("    Creation Date:     {0}", myPDF.DocumentInformation.CreationDate)
            log.Stream.WriteLine("    Modification Date: {0}", myPDF.DocumentInformation.GetModificationDate)

            If myPDF.ViewerPreferences Is Nothing Then
                'If the document do not contain preferences, with create them
                myPDF.CreateViewerPreferences()
            End If

            'We add some viewer preferences
            myPDF.ViewerPreferences.HideWindowUI = True
            myPDF.ViewerPreferences.CenterWindow = True
            myPDF.ViewerPreferences.HideMenubar = True
            myPDF.ViewerPreferences.HideToolbar = True
            myPDF.ViewerPreferences.NumCopies = 5
            myPDF.ViewerPreferences.NonFullScreenPageMode = NonFullScreenPageModeTypes.UseThumbs
            myPDF.ViewerPreferences.Duplex = DuplexTypes.Simplex
            myPDF.ViewerPreferences.DisplayDocTitle = True
            myPDF.ViewerPreferences.PickTrayByPDFSize = True
            myPDF.ViewerPreferences.PrintScaling = PrintScalingTypes.AppDefault
            myPDF.ViewerPreferences.PageLayout = PageLayoutTypes.TwoPageLeft
            myPDF.ViewerPreferences.PageMode = PageModeTypes.UseThumbs


            Dim page As pdflib.clsPDFPage
            'Get page 1 of the PDF
            page = myPDF.GetPage(1)
            If Not page Is Nothing Then
                'Display page information
                log.Stream.WriteLine("  information for the first page:")
                log.Stream.WriteLine("    User Unit:  {0}", page.UserUnit)
                log.Stream.WriteLine("    Page Width: {0}", page.Width)
                log.Stream.WriteLine("    Page Height: {0}", page.Height)
                log.Stream.WriteLine("    Page Rotation: {0}", page.Rotation)

            End If

            'We get the images of the pdf from the fist 10 pages
            'We save them in separate folder
            Dim count, nbrPage As Integer
            Dim imagelist As List(Of clsPDFImage)
            Dim image As clsPDFImage

            Dim dir As New DirectoryInfo(My.Resources.dir_image_saved + myPDF.FileInfo.Name)
            If Not dir.Exists Then
                dir.Create()
            End If

            nbrPage = 0
            For Each page In myPDF.GetPages
                If nbrPage = 10 Then
                    Exit For
                End If

                imagelist = page.GetImages
                count = 0
                For Each image In imagelist
                    count += 1
                    image.Save(dir.FullName + "\" + "image_" + count.ToString)
                Next
                nbrPage += 1
            Next

            'We create the annotations
            Dim annot_1 As New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
            annot_1.Contents = "This is an annotation"
            annot_1.Location(300, 300, 300, 300)

            Dim annot_2 As New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
            annot_2.SetCharacteristic = pdflib.AnnotationFlags.Locked Or pdflib.AnnotationFlags.ReadOnlyFlag
            annot_2.Contents = "Another one (This one is locked!)"
            annot_2.Location(200, 200, 200, 200)

            If Not page Is Nothing Then
                'Add two annotations to the page

                page.AddAnnotation(annot_1)
                page.AddAnnotation(annot_2)

                'Add an image
                page.AddImage(image1, 250, 250, 50, 50)
            End If

            page = myPDF.GetPage(2)
            If Not page Is Nothing Then
                'Add another image on page 2
                page.AddImage(image2, 100, 100, 100, 100)
            End If

            page = myPDF.GetPage(3)
            If Not page Is Nothing Then
                'Rotate page 3
                page.Rotation = pdflib.PageRotationTypes.Rotation_90
            End If

            page = myPDF.GetPage(4)
            If Not page Is Nothing Then
                'Erase content of page 4
                page.EraseContent()
            End If

            myPDF.DocumentInformation.Keywords = "Test"
            myPDF.DocumentInformation.Author = "Unknown"
            myPDF.DocumentInformation.Title = "Titre"
            myPDF.DocumentInformation.Producer = "pdflib 1.1"
            myPDF.DocumentInformation.Subject = "sujet"
            myPDF.DocumentInformation.Creator = "Unknown"
            'pdf.Save()
            myPDF.Save(My.Resources.dir_pdf_saved + myPDF.FileInfo.Name)
        End If
        myPDF.Close()

        myPDF = Nothing
    End Sub
    ''' <summary>
    ''' I use this to debug pdflib for a specific PDF
    ''' </summary>
    ''' <param name="filePath"></param>
    ''' <remarks></remarks>
    Private Sub DebugTest(ByVal filePath As String)
        Dim page As pdflib.clsPDFPage

        Dim myPDF As New pdflib.ClsPDF(filePath)
        page = myPDF.GetPage(1)
        If Not page Is Nothing Then
            Dim annotation As New pdflib.ClsPdfAnnotation(pdflib.AnnotationTypes.Text)
            annotation.Contents = "This is an annotation"
            annotation.Location(300, 300, 300, 300)
            page.AddAnnotation(annotation)

            page.AddImage(image2, 0, 0, 500, 500)
        End If
        If Not page Is Nothing Then
            'page.AddImage(image2, 300, 300, 100, 100)
            'page.AddImage(image1, 500, 100, 50, 50)
        End If
        'page = myPDF.PageInformation(3)
        'If Not page Is Nothing Then
        '    page.AddImage(image2, 300, 300, 100, 100)
        'End If
        myPDF.DocumentInformation.Keywords = "some keywords goes here"
        myPDF.DocumentInformation.Title = "Title please"
        myPDF.DocumentInformation.Author = "Charles"
        myPDF.DocumentInformation.Creator = "PdfLib"
        myPDF.DocumentInformation.Producer = "PdfLib"
        myPDF.DocumentInformation.Subject = "Some unknown subject"
        myPDF.Save(My.Resources.dir_pdf_saved + myPDF.FileInfo.Name)
        myPDF.Close()
    End Sub
End Module
